/**
 * <p>描述: [类型描述] </p>
 *
 * @author liugl
 * @version v1.0
 */
package cn.ztr.familyalbum.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * json字段映射,过滤转驼峰工具类
 *
 * @Author: liugl
 * @Date: 2020/2/28 14:42
 */
public class JsonConvertKeyUtil {

    private JsonConvertKeyUtil() {
        throw new IllegalStateException("Utility class");
    }
    /**
     * 转换核心(过略规则),key驼峰转换,value去首位空格
     *
     * @param object
     * @return
     */
    public static JSONObject covertObject(JSONObject object) {
        if (object == null) {
            return null;
        }
        JSONObject newObject = new JSONObject();
        Set<String> set = object.keySet();
        for (String key : set) {
            Object value = object.get(key);
            if (value instanceof JSONArray) {
                //数组
                value = covertArray(object.getJSONArray(key));
            } else if (value instanceof JSONObject) {
                //对象
                value = covertObject(object.getJSONObject(key));
            } else if (value instanceof String){
                value = object.getString(key).trim();
            }
            key = strTohump(key);
            newObject.put(key, value);
        }
        return newObject;
    }

    /**
     * 转换核心(过略规则),key驼峰转换,value去首位空格
     *
     * @param object
     * @return
     */
    public static JSONObject covertObjectOnlyValue(JSONObject object) {
        if (object == null) {
            return null;
        }
        JSONObject newObject = new JSONObject();
        Set<String> set = object.keySet();
        for (String key : set) {
            Object value = object.get(key);
            if (value instanceof JSONArray) {
                //数组
                value = covertArray(object.getJSONArray(key));
            } else if (value instanceof JSONObject) {
                //对象
                value = covertObject(object.getJSONObject(key));
            } else if (value instanceof String){
                //值字段过略规证,(转为string)去除前后空格
                value = object.getString(key).trim();
            }
            newObject.put(key, value);
        }
        return newObject;
    }


    /**
     * 转换核心(过略规则),key转换小写
     *
     * @param object
     * @return
     */
    public static JSONObject covertObjectOnlyKeyLowercase(JSONObject object) {
        if (object == null) {
            return null;
        }
        JSONObject newObject = new JSONObject();
        Set<String> set = object.keySet();
        for (String key : set) {
            Object value = object.get(key);
            if (value instanceof JSONArray) {
                //数组
                value = covertArray(object.getJSONArray(key));
            } else if (value instanceof JSONObject) {
                //对象
                value = covertObject(object.getJSONObject(key));
            } else if (value instanceof String){
                //值字段过略规证,(转为string)去除前后空格
                value = object.getString(key).toLowerCase();
            }
            newObject.put(key, value);
        }
        return newObject;
    }

    public static JSONArray covertArray(JSONArray array) {
        if (array == null) {
            return null;
        }
        JSONArray newArray = new JSONArray();
        for (int i = 0; i < array.size(); i++) {
            Object value = array.get(i);
            if (value instanceof JSONArray) {
                //数组
                value = covertArray(array.getJSONArray(i));
            } else if (value instanceof JSONObject) {
                //对象
                value = covertObject(array.getJSONObject(i));
            }
            newArray.add(value);
        }
        return newArray;
    }


    /**
     * 过滤json对象
     * @param jsonObject
     * @return jsonObject
     */
    public static JSONObject convertJsonKeyRetrunString(JSONObject jsonObject) {
        return covertObject(jsonObject);
    }

    /**
     * 过滤json对象
     * @param jsonObject
     * @return jsonObject
     */
    public static JSONObject convertJsonValueRetrunString(JSONObject jsonObject) {
        return  covertObjectOnlyValue(jsonObject);
    }

    public static String strTohump(String str) {
        StringBuilder buffer = new StringBuilder();
        if (str != null && str.length() > 0) {
            String s1 = "_";
            if (str.contains(s1)) {
                String[] chars = str.split(s1);
                int size = chars.length;
                if (size > 0) {
                    List<String> list = new ArrayList();
                    for (String s : chars) {
                        if (s != null && s.trim().length() > 0) {
                            list.add(s);
                        }
                    }
                    size = list.size();
                    if (size > 0) {
                        buffer.append(lowerCase(list.get(0)));

                        for (int i = 1; i < size; i++) {
                            String s = list.get(i);
                            buffer.append(upperCase(s.substring(0, 1)));
                            if (s.length() > 1) {
                                buffer.append(s.substring(1));
                            }
                        }
                    }
                }
            } else {
                buffer.append(str);
            }
        }

        return buffer.toString();
    }

    public static String upperCase(String str) {
        char[] ch = str.toCharArray();
        char a = 'a';
        char z = 'z';
        if (ch[0] >= a && ch[0] <= z) {
            ch[0] = (char) (ch[0] - 32);
        }
        return new String(ch);
    }

    public static String lowerCase(String str) {
        char[] ch = str.toCharArray();
        char a = 'A';
        char z = 'Z';
        if (ch[0] >= a && ch[0] <= z) {
            ch[0] = (char) (ch[0] + 32);
        }
        return new String(ch);
    }
}